// 配合测试用户中心部门树的搜索方法

let arr = [1, 3, 5, 7, 9, 3, 5, 3, 3, 3, 3, 3, 3, 3]

let brr = arr.filter((item, i, self) => item && self.indexOf(item) === i)

// console.log(brr)

let str1 = '客服中心'
let val = '心'
let num = str1.indexOf(val)
// console.log(num)

let treeData = [
  {
    name: '国家电网',
    treeid: '0-0',
    children: [
      {
        name: '信息运维中心',
        treeid: '0-0-0',
        children: [
          { name: '系统研发部', treeid: '0-0-0-0' },
          { name: '运行检修部', treeid: '0-0-0-1' }
        ]
      },
      {
        name: '客服中心',
        treeid: '0-0-1',
        children: [
          { name: '客服一部', treeid: '0-0-1-0' },
          { name: '客服二部', treeid: '0-0-1-1' }
        ]
      }
    ]
  }
]

const getParentKey = (key, tree) => {
  let parentKey
  for (let i = 0; i < tree.length; i++) {
    const node = tree[i]
    if (node.children) {
      if (node.children.some(item => item.treeid === key)) {
        parentKey = node.treeid
        // 如果这个children还有children，级继续循环
      } else if (getParentKey(key, node.children)) {
        parentKey = getParentKey(key, node.children)
      }
    }
  }
  // 如果传入的id没有父级，它本身就是最高级，就会返回undefinde
  return parentKey
}

// console.log(getParentKey('0-0',treeData));


let arr3 = [
  {
    name: 'tom',
    age: 10
  },
  {
    name: 'tom1',
    age: 20
  },
  {
    name: 'tom',
    age: 30
  },
  {
    name: 'tom',
    age: 40
  }
]

function unique (arr3) {
  const res = new Map()
  return arr3.filter((item) => !res.has(item.name) && res.set(item.name, item.age))
}
console.log(unique(arr3)); // [ { name: 'tom', age: 10 }, { name: 'tom1', age: 20 } ]